package br.mg.bhe.ftpobre.command;

import java.io.IOException;
import java.util.logging.Logger;

import br.mg.bhe.ftpobre.ConnectionData;
import br.mg.bhe.ftpobre.Main;
import br.mg.bhe.ftpobre.log.FtpLogView;
import br.mg.bhe.ftpobre.util.LogUtil;

/**
 * 
 * @author Aureliano
 */
public class DisconnectionCommand implements ICommand {

	public void execute() {
		if (this.canExecute()) {
            this.doAction();
        }
	}

	public boolean canExecute() {
		return Main.getConnectionData().isConnected();
	}
	
	private void doAction() {
		ConnectionData c = Main.getConnectionData();
		FtpLogView.log("Fechando conexão com " + c.getHost());
		Logger logger = LogUtil.getLogger(getClass());
		
		try {
			c.getFtpClient().disconnect();
			c.setWorkingOnline(false);
			FtpLogView.log("Desconectado!");
		} catch (IOException ex) {
			FtpLogView.log(ex.getMessage());
			logger.severe(ex.getMessage());
			throw new RuntimeException(ex);
		}
	}
}